# -*- coding: utf-8 -*-

"""
Time    :2021/6/15 下午9:41
Author  :chenyang
Email   :chenyang.it@outlook.com
FileName: devices.py
Software: PyCharm
"""
from datetime import datetime
from werkzeug.security import generate_password_hash, check_password_hash

from app.extensions import db


class Devices(db.Model):
    __tablename__ = 'devices'
    sid = db.Column(db.String(64), primary_key=True)
    model = db.Column(db.String(64))
    cpu_model = db.Column(db.String(64))
    cpu_cores = db.Column(db.String(64))
    bmc_address = db.Column(db.String(64))
    purpose = db.Column(db.String(64))
    owner = db.Column(db.String(64))
    others = db.Column(db.Text)

    def __init__(self, sid=None, model=None, cpu_model=None, cpu_cores=None, bmc_address='192.168.2.10', purpose=None,
                 owner=None, others=None):
        self.sid = sid
        self.model = model
        self.cpu_model = cpu_model
        self.cpu_cores = cpu_cores
        self.bmc_address = bmc_address
        self.purpose = purpose
        self.owner = owner
        self.others = others


# 管理员表
class Admin(db.Model):
    __tablename__ = 'admin'
    uid = db.Column(db.String(64), primary_key=True)
    name = db.Column(db.String(255), nullable=False)
    password_hash = db.Column(db.String(255), nullable=False)
    email = db.Column(db.String(255), nullable=False)
    create_time = db.Column(db.DateTime, default=datetime.now)

    def __init__(self, uid, name, password, email):
        self.uid = uid
        self.name = name
        self.password = password
        self.email = email

    @property
    def password(self):
        return self.password_hash

    @password.setter
    def password(self, value):
        self.password_hash = generate_password_hash(value)

    # 验证密码是否正确
    def check_password(self, password):
        return check_password_hash(self.password_hash, password)
